﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using Remind.BLL;
using System.Configuration;

namespace Remind.View
{
    public partial class FrmReports : Form
    {
        //每页显示行数
        int pageSize = 0;
        //总记录数
        int nMax = 0;
        //页数＝总记录数/每页显示行数
        int pageCount = 0;
        //当前页号
        int pageCurrent = 0;
        //当前记录行
        int nCurrent = 0;

        DataSet ds = new DataSet();
        DataTable dtInfo = new DataTable();

        private readonly GroupBLL gBLL = new GroupBLL();

        private readonly string strConn = ConfigurationManager.ConnectionStrings["Contacts"].ConnectionString + ConfigurationManager.AppSettings["database"] + "';Jet OLEDB:Database Password=283713763@qq.com;";

        public FrmReports()
        {
            InitializeComponent();
        }

        private void FrmReports_Load(object sender, EventArgs e)
        {
            string strSql = "SELECT cid, cname, IIf(csex=True,'男','女') AS sex,cbirthday, DateDiff('yyyy',contacts.cbirthday,Now()) AS age, idcard,cbz FROM contacts";
            ExceSql(strSql);
            tsddlGroup.ComboBox.DataSource = gBLL.GetGroup();
            tsddlGroup.ComboBox.DisplayMember = "Gname";
            tsddlGroup.ComboBox.ValueMember = "Gid";
        }

        private void ExceSql(string sql)
        {
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            OleDbDataAdapter sda = new OleDbDataAdapter(sql, conn);
            sda.Fill(ds, "ds");
            conn.Close();
            dtInfo = ds.Tables[0];
            //if (dtInfo.Rows.Count > 0)
            //{
                this.dgvContacts.Columns["cbirthday"].DefaultCellStyle.Format = "D";
                InitDataSet();
            //}
        }

        private void InitDataSet()
        {
            //pageSize = 20;      //设置页面行数
            //nMax = dtInfo.Rows.Count;

            //pageCount = (nMax / pageSize);    //计算出总页数

            //if ((nMax % pageSize) > 0) pageCount++;

            //pageCurrent = 1;    //当前页数从1开始
            //nCurrent = 0;         //当前记录数从0开始

            LoadData();
        }

        private void LoadData()
        {
            //int nStartPos = 0;   //当前页面开始记录行
            //int nEndPos = 0;     //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            //if (pageCurrent == pageCount)
            //    nEndPos = nMax;
            //else
            //    nEndPos = pageSize * pageCurrent;

            //nStartPos = nCurrent;

            //bindingNavigatorCountItem.Text = pageCount.ToString();
            //bindingNavigatorPositionItem.Text = Convert.ToString(pageCurrent);

            ////从元数据源复制记录行
            if (dtInfo.Rows.Count > 0)
            {
                for (int i = 0; i < dtInfo.Rows.Count; i++)
                {
                    dtTemp.ImportRow(dtInfo.Rows[i]);
                    nCurrent++;
                }
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvContacts.DataSource = bdsInfo;
        }

        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //switch (e.ClickedItem.Text)
            //{
            //    case "首页":
            //        pageCurrent = 1;
            //        nCurrent = 0;
            //        LoadData();
            //        //禁用上页和首页
            //        bindingNavigatorMovePreviousItem.Enabled = false;
            //        bindingNavigatorMoveFirstItem.Enabled = false;
            //        if (!bindingNavigatorMoveNextItem.Enabled)
            //        {
            //            bindingNavigatorMoveNextItem.Enabled = true;
            //            bindingNavigatorMoveLastItem.Enabled = true;
            //        }
            //        break;
            //    case "上一页":
            //        pageCurrent--;
            //        if (pageCurrent <= 0)
            //        {
            //            //禁用上页和首页
            //            bindingNavigatorMovePreviousItem.Enabled = false;
            //            bindingNavigatorMoveFirstItem.Enabled = false;
            //            return;
            //        }
            //        else
            //        {
            //            if (!bindingNavigatorMoveNextItem.Enabled)
            //            {
            //                bindingNavigatorMoveNextItem.Enabled = true;
            //                bindingNavigatorMoveLastItem.Enabled = true;
            //            }
            //            nCurrent = pageSize * (pageCurrent - 1);
            //        }
            //        LoadData();
            //        break;
            //    case "下一页":
            //        pageCurrent++;
            //        if (pageCurrent > pageCount)
            //        {
            //            bindingNavigatorMoveNextItem.Enabled = false;
            //            bindingNavigatorMoveLastItem.Enabled = false;
            //            return;
            //        }
            //        else
            //        {
            //            if (!bindingNavigatorMovePreviousItem.Enabled)
            //            {
            //                bindingNavigatorMovePreviousItem.Enabled = true;
            //                bindingNavigatorMoveFirstItem.Enabled = true;
            //            }
            //            nCurrent = pageSize * (pageCurrent - 1);
            //        }
            //        LoadData();
            //        break;
            //    case "末页":
            //        pageCurrent = pageCount;
            //        nCurrent = (pageSize * pageCurrent) - pageSize;
            //        LoadData();
            //        bindingNavigatorMoveNextItem.Enabled = false;
            //        bindingNavigatorMoveLastItem.Enabled = false;
            //        if (!bindingNavigatorMovePreviousItem.Enabled)
            //        {
            //            bindingNavigatorMovePreviousItem.Enabled = true;
            //            bindingNavigatorMoveFirstItem.Enabled = true;
            //        }
            //        break;
            //    default:
            //        break;
            //}
        }

        private void tsddlGroup_DropDownClosed(object sender, EventArgs e)
        {
            dtInfo.Clear();
            string s = tsddlGroup.ComboBox.SelectedValue.ToString();
            string strSql = "SELECT cid, cname, IIf(csex=True,'男','女') AS sex,cbirthday, DateDiff('yyyy',contacts.cbirthday,Now()) AS age, idcard,cbz FROM contacts where gid=" + s;
            ExceSql(strSql);
        }

        private void tsbtnAll_Click(object sender, EventArgs e)
        {
            dtInfo.Clear();
            string strSql = "SELECT cid, cname, IIf(csex=True,'男','女') AS sex,cbirthday, DateDiff('yyyy',contacts.cbirthday,Now()) AS age, idcard,cbz FROM contacts";
            ExceSql(strSql);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            dgvContacts.EndEdit();

            using (OleDbConnection connection = new OleDbConnection(strConn))
            {
                OleDbCommand command = new OleDbCommand();
                OleDbTransaction transaction = null;
                //设置连接对象
                command.Connection = connection;
                try
                {
                    connection.Open();
                    transaction = connection.BeginTransaction();
                    command.Connection = connection;
                    command.Transaction = transaction;
                    int j = 0;
                    for (int i = 0; i < dgvContacts.Rows.Count; i++)
                    {
                        if (dgvContacts.Rows[i].Cells["chk"].Value != null) //判断该行的复选框是否存在 
                        {
                            if (dgvContacts.Rows[i].Cells["chk"].Value.ToString() == "True") //判断该复选框是否被选中 
                            {
                                command.CommandText =
                                    "delete from contacts where cid=" + dgvContacts.Rows[i].Cells["cid"].Value.ToString();
                               command.ExecuteNonQuery();
                            }
                            j++;
                        }
                    }
                    if (j == 0)
                    {
                        MessageBox.Show("请选择要删除的联系人!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        if (MessageBox.Show("删除联系人将删除该联系人的地址,电话,网络信!\t\r息是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                }
            }
            //重新查询
            dtInfo.Clear();
            string strSql = "SELECT cid, cname, IIf(csex=True,'男','女') AS sex,cbirthday, DateDiff('yyyy',contacts.cbirthday,Now()) AS age, idcard,cbz FROM contacts";
            ExceSql(strSql);
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvContacts.Rows.Count; i++)
            {
                dgvContacts.Rows[i].Cells["chk"].Value = dgvContacts.Rows[i].Cells["chk"].Value == null || !(bool)dgvContacts.Rows[i].Cells["chk"].Value ? true : false;
            }
        }
    }
}

